package com.soft.sitegenerator.action;

import java.io.File;
import java.io.FileOutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import com.soft.sitegenerator.form.StrutsUploadForm;
/**
* @author Deepak Kumar
* @Web http://www.roseindia.net
* @Email roseindia_net@yahoo.com
*/

/**
 * Struts File Upload Action Form.
 *
*/
public class StrutsUploadAction extends Action
{
  public ActionForward execute(
    ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response) throws Exception{
    StrutsUploadForm myForm = (StrutsUploadForm)form;


    FormFile myFile = myForm.getTheFile();
    String contentType = myFile.getContentType();
    String fileName    = myFile.getFileName();
    byte[] fileData    = myFile.getFileData();

    String filePath = getServlet().getServletContext().getRealPath("/") +"resources";

	if(!fileName.equals("")){  
	    File fileToCreate = new File(filePath, fileName);
                      
	    if(!fileToCreate.exists()){
	      FileOutputStream fileOutStream = new FileOutputStream(fileToCreate);
	      fileOutStream.write(myFile.getFileData());
	      fileOutStream.flush();
	      fileOutStream.close();
	    }  
	
	
	}
	  
    return mapping.findForward("success");
  }
}